package com.tuanzi.loan.business.service.sms.el.evaluator;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.service.sms.el.AbstractEvaluator;
import com.tuanzi.server.loan_base.util.BirthdayUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class BaseInfoEvaluator extends AbstractEvaluator {

	/*
	 * 年龄大于
	 */
	public BaseInfoEvaluator ageGreaterThan(Integer age) {
		log.info("ageGreaterThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream().filter(x -> {
			Integer innerAge = 0;
			try {
				innerAge = BirthdayUtils.calculateAgeFromIdCard(x.getIdcard());
				innerAge = innerAge == null ? 0 : innerAge;
			} catch (Exception e) {
				// 容错处理
			}
			return innerAge >= age;
		}).collect(Collectors.toList()));
		log.info("ageGreaterThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 年龄少于
	 */
	public BaseInfoEvaluator ageLessThan(Integer age) {
		log.info("ageLessThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream().filter(x -> {
			Integer innerAge = 0;
			try {
				innerAge = BirthdayUtils.calculateAgeFromIdCard(x.getIdcard());
				innerAge = innerAge == null ? 0 : innerAge;
			} catch (Exception e) {
				// 容错处理
			}
			return innerAge <= age;
		}).collect(Collectors.toList()));
		log.info("ageLessThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 芝麻分大于
	 */
	public BaseInfoEvaluator sesamePointGreaterThan(Integer sesamePoint) {
		log.info("sesamePointGreaterThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext().setResponses(
				getMatchRuleContext().getResponses().stream().filter(x -> x.getSesamePoint() != null && x.getSesamePoint() >= sesamePoint).collect(Collectors.toList()));
		log.info("sesamePointGreaterThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 芝麻分少于
	 */
	public BaseInfoEvaluator sesamePointLessThan(Integer sesamePoint) {
		log.info("sesamePointLessThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext().setResponses(
				getMatchRuleContext().getResponses().stream().filter(x -> x.getSesamePoint() != null && x.getSesamePoint() <= sesamePoint).collect(Collectors.toList()));
		log.info("sesamePointLessThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 意向金额大于
	 */
	public BaseInfoEvaluator amountGreaterThan(Integer amount) {
		log.info("amountGreaterThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext()
				.setResponses(getMatchRuleContext().getResponses().stream().filter(x -> x.getAmount() != null && x.getAmount() >= amount).collect(Collectors.toList()));
		log.info("amountGreaterThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 意向金额少于
	 */
	public BaseInfoEvaluator amountLessThan(Integer amount) {
		log.info("amountLessThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext()
				.setResponses(getMatchRuleContext().getResponses().stream().filter(x -> x.getAmount() != null && x.getAmount() <= amount).collect(Collectors.toList()));
		log.info("amountLessThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 月收入大于
	 */
	public BaseInfoEvaluator incomeGreaterThan(Integer income) {
		log.info("incomeGreaterThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext()
				.setResponses(getMatchRuleContext().getResponses().stream().filter(x -> x.getIncome() != null && x.getIncome() >= income).collect(Collectors.toList()));
		log.info("incomeGreaterThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 月收入少于
	 */
	public BaseInfoEvaluator incomeLessThan(Integer income) {
		log.info("incomeLessThan filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext()
				.setResponses(getMatchRuleContext().getResponses().stream().filter(x -> x.getIncome() != null && x.getIncome() <= income).collect(Collectors.toList()));
		log.info("incomeLessThan filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 性别
	 */
	public BaseInfoEvaluator sex(List<String> sexs) {
		log.info("sex filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(sexs);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream().filter(x -> (StringUtils.isBlank(x.getSex()) && isAllowEmpty) || sexs.contains(x.getSex()))
				.collect(Collectors.toList()));
		log.info("sex filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 婚姻状态
	 */
	public BaseInfoEvaluator marriage(List<String> marriages) {
		log.info("marriages filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(marriages);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getMarriage()) && isAllowEmpty) || marriages.contains(x.getMarriage())).collect(Collectors.toList()));
		log.info("marriages filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 信用卡
	 */
	public BaseInfoEvaluator credit(List<String> credis) {
		log.info("credis filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(credis);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getCredit()) && isAllowEmpty) || credis.contains(x.getCredit())).collect(Collectors.toList()));
		log.info("credis filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 社保状态
	 */
	public BaseInfoEvaluator socialInsurance(List<String> socialInsurances) {
		log.info("socialInsurance filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(socialInsurances);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getSocialInsurance()) && isAllowEmpty) || socialInsurances.contains(x.getSocialInsurance())).collect(Collectors.toList()));
		log.info("socialInsurance filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 房公积金状态
	 */
	public BaseInfoEvaluator houseFund(List<String> houseFunds) {
		log.info("houseFund filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(houseFunds);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getHouseFund()) && isAllowEmpty) || houseFunds.contains(x.getHouseFund())).collect(Collectors.toList()));
		log.info("houseFund filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 车辆状态
	 */
	public BaseInfoEvaluator car(List<String> cars) {
		log.info("car filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(cars);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getHasCar()) && isAllowEmpty) || cars.contains(x.getHasCar())).collect(Collectors.toList()));
		log.info("car filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 房屋状态
	 */
	public BaseInfoEvaluator house(List<String> houses) {
		log.info("house filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(houses);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getHasHouse()) && isAllowEmpty) || houses.contains(x.getHasHouse())).collect(Collectors.toList()));
		log.info("house filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 公司名称包含'有限公司'
	 */
	public BaseInfoEvaluator containsLimitationCompany() {
		log.info("containsLimitationCompany filter before" + getMatchRuleContext().getResponses().size());
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream().filter(x -> StringUtils.contains(x.getCompany(), "有限公司")).collect(Collectors.toList()));
		log.info("containsLimitationCompany filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	/*
	 * 职业身份
	 */
	public BaseInfoEvaluator profession(List<String> professions) {
		log.info("professions filter before" + getMatchRuleContext().getResponses().size());
		boolean isAllowEmpty = isAllowEmpty(professions);
		getMatchRuleContext().setResponses(getMatchRuleContext().getResponses().stream()
				.filter(x -> (StringUtils.isBlank(x.getProfession()) && isAllowEmpty) || professions.contains(x.getProfession())).collect(Collectors.toList()));
		log.info("professions filter after" + getMatchRuleContext().getResponses().size());
		return this;
	}

	private boolean isAllowEmpty(List<String> values) {
		return values.contains("没填写");
	}

}
